<template>
    <div class="musicList">
        <!-- 发现好歌单 -->
        <div class="musicList-top">
            <div class="title">发现好歌单</div>
            <div class="more">查看更多</div>
        </div>
        <!-- 列表 -->
        <div class="mlist">
            <!-- Swiper -->
            <div class="swiper-container" id="musicSwiper">
                <div class="swiper-wrapper">
                    <router-link :to="{path:'/listview',query:{id:item.id}}" class="swiper-slide" v-for="(item, i) in state.musicList" :key="i">
                        <img :src="item.picUrl" :alt="item.name">
                        <div class="name">{{item.name}}</div>
                        <div class="count">
                            <svg class="icon" aria-hidden="true">
                                <use xlink:href="#icon-bofang1"></use>
                            </svg>
                            <span>{{changsValue(item.playCount)}}</span>
                        </div>
                    </router-link>
                </div>
            </div>
            <!-- Swiper -->
        </div>
    </div>
</template>

<script>
// import style (<= Swiper 5.x) swiper 5版本
import 'swiper/css/swiper.css'
//导入swiper
import Swiper from 'swiper';
//导入网络请求
import {getMusicList} from '@/api/index.js'
import {reactive, onMounted, onUpdated} from 'vue'

export default {
    name: "musicList",
    setup() {
        let state = reactive({musicList:[]})
        function changsValue(num) {
            let res = 0
            if(num>=100000000) {
                res = num / 100000000
                // toFixed(2)  vue中toFixed获取小数点后两位
                res = res.toFixed(2) + '亿'
            }else if(num>10000){
                res = num/10000
                // toFixed(2)  vue中toFixed获取小数点后两位
                res = res.toFixed(2) + '万'
            }
            return res
        }
        onMounted(async () => {
            let result = await getMusicList()
            state.musicList = result.data.result;
        })
        onUpdated(() => {
            var swiper = new Swiper('#musicSwiper', {
            slidesPerView: 3,
            spaceBetween: 10,
            });
        })
        return {
            state,
            changsValue
        }
    }
}

// vue2的写法
// export default {
//     name: "musicList",
//     data() {
//         return {
//             musicList: []
//         }
//     },
//     methods:{
//         changsValue:function(num) {
//             let res = 0
//             if(num>=100000000) {
//                 res = num / 100000000
//                 // toFixed(2)  vue中toFixed获取小数点后两位
//                 res = res.toFixed(2) + '亿'
//             }else if(num>10000){
//                 res = num/10000
//                 // toFixed(2)  vue中toFixed获取小数点后两位
//                 res = res.toFixed(2) + '万'
//             }
//             return res
//         }
//     },
//     mounted:async function() {
//         let result = await getMusicList()
//         this.musicList = result.data.result;
//     },
//     updated() {
//         var swiper = new Swiper('#musicSwiper', {
//         slidesPerView: 3,
//         spaceBetween: 10,
//         });
//     },
// }
</script>

<style lang="less">
.musicList {
    width: 7.5rem;
    padding: 0 0.4rem;
    .musicList-top {
        display: flex;
        // 两边平均分布，
        justify-content: space-between;
        height: 1rem;
        align-items: center;
        .title {
            font-size: 0.4rem;
            font-weight: 900;
        }
        .more {
            height: 0.5rem;
            width: 1.2rem;
            text-align: center;
            line-height: 0.5rem;
            border: 1px solid #ccc;
            border-radius: 0.2rem;
            font-size: 0.24rem;
        }
    }
    .mlist {
        .swiper-container {
            width: 100%;
            height: 3rem;
            .swiper-slide {
            // background-color: pink;
            display: flex;
            //垂直布局
            flex-direction: column;
            position: relative;
                img {
                    width: 100%;
                    height: auto;
                    border-radius: 0.1rem;
                }
                .name {
                    height: 0.6rem;
                    width: 100%;
                    font-size: 0.24rem;
                    line-height: 0.4rem;
                }
                .count {
                    position: absolute;
                    display: flex;
                    align-items: center;
                    right: 0.1rem;
                    top: 0.1rem;
                    font-size: 0.24rem;
                    color: #ccc;
                    .icon {
                        fill: #ccc;
                    }
                }
            }
        }
    }
}
</style>